﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Configuration;


namespace de.rwth.rz.campus.Mailer
{
    /// <remarks>
    /// 
    /// Copyright 2010 RWTH Aachen, CAMPUS Labs
    /// 
    /// Licensed under the Apache License, Version 2.0 (the "License");
    /// you may not use this file except in compliance with the License.
    /// You may obtain a copy of the License at
    ///
    /// http://www.apache.org/licenses/LICENSE-2.0
    ///
    /// Unless required by applicable law or agreed to in writing, software
    /// distributed under the License is distributed on an "AS IS" BASIS,
    /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    /// See the License for the specific language governing permissions and
    /// limitations under the License.
    /// 
    /// </remarks>

    [CLSCompliant(false)]
    public class FormatField : SPFieldChoice
    {
        public FormatField(SPFieldCollection fields, string fieldName) :

            base(fields, fieldName) { }

        public FormatField(SPFieldCollection fields, string typeName, string displayName) :

            base(fields, typeName, displayName) { }

        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl fieldControl = new FormatFieldControl();
                fieldControl.FieldName = InternalName;
                return fieldControl;
            }
        }

        // Validate the string. If not valid, throw an SPFieldValidationException
        public override string GetValidatedString(object value)
        {
            return base.GetValidatedString(value);
        }

    }

    [CLSCompliant(false)]
    public class FormatFieldControl : BaseFieldControl
    {
        protected DropDownList FormatDrpDwn;


        protected override string DefaultTemplateName
        {
            get
            {
                return @"FormatFieldControl";
            }
        }

        public override object Value
        {
            get
            {
                this.EnsureChildControls();
                return this.FormatDrpDwn.SelectedValue;
            }
            set
            {
                EnsureChildControls();
                this.FormatDrpDwn.SelectedValue = (string)this.ItemFieldValue;
            }
        }


        protected override void CreateChildControls()
        {
            if (this.Field == null || this.ControlMode == SPControlMode.Display)
                return;
            base.CreateChildControls();

            FormatDrpDwn = (DropDownList)TemplateContainer.FindControl("FormatSelector");
            if (this.FormatDrpDwn == null)
                throw new ConfigurationErrorsException("Corrupted Controltemplate file.");

            if (!this.Page.IsPostBack)
            {
                this.FormatDrpDwn.Items.AddRange(new ListItem[]{ 
                    new ListItem("HTML", "HTML"), 
                    new ListItem("TEXT", "TEXT")});
            }
        }

    }
}
